<!--* Created by caiqi on 16/12/19.-->
<template>
  <div class="docu">
    <div class="title">{{detailData.title}}</div>
    <div class="writer">作者：{{detailData.writer}}</div>
    <div class="content" v-html="detailData.content"></div>
    <van-button :class="like?'like':'unlike'" round size="small" @click="toLike">喜欢</van-button>
  </div>
</template>

<script>
import Vue from "vue";
import { Toast, Button, Icon } from "vant";

import api from "@/api/index";

Vue.use(Toast)
  .use(Button)
  .use(Icon);

export default {
  components: {},
  data() {
    return {
      like: false,
      readWatch: {},
      detailId: this.$route.params.id,
      detailData: {}
    };
  },
  methods: {
    // ========================================  获取文章列表
    async getDocDetails() {
      const { code, data } = await api.getDocDetails(this.detailId);
      if (code === 0) {
        this.detailData = data;
        window.document.title = data.title;
      }
    },
    // ========================================  设置阅读数
    async setRed() {
      const { code } = await api.getDocRead(this.detailId);
      if (code === 0) {
        Toast("如果对你有用就在下方点个喜欢吧~");
      }
    },
    // ========================================  设置喜欢数
    async toLike() {
      if (this.like) {
        return;
      }
      const { code } = await api.getDocLike(this.detailId);
      if (code === 0) {
        this.like = true;
        Toast("感谢你喜欢这篇内容~");
      }
    }
  },
  computed: {},
  mounted() {
    this.getDocDetails();
    this.readWatch = setTimeout(() => {
      this.setRed();
    }, 3000);
    // this.getTradList();
  },

  watch: {},
  beforeRouteLeave: function(to, from, next) {
    clearTimeout(this.readWatch);
    next();
  }
};
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
.docu {
  .title {
    padding-top: 10px;
    font-size: 20px;
    text-align: center;
  }
  .writer {
    margin-top: 12px;
    text-align: center;
    font-size: 12px;
    color: #bbbbbb;
  }
  .content {
    padding: 0 15px;
  }
  .unlike {
    display: block;
    margin: 0 auto;
    margin-bottom: 20px;
  }
  .like {
    @extend .unlike;
    color: #fff;
    background-color: #e69f80;
  }
}
</style>